﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMTC.Model.Model;
using SMTC.BO.Plan;
using SMTC.WebCommon.Common;
using SMTC.Model.Domain;
using SMTC.BO.BaseInfo;
using SMTC.BO.DataServer;

public partial class PlanMangement_PlanView : BasePage
{
    ProductBaseInfoServices _pservices = new ProductBaseInfoServices();
    DataServerServices _dataService = new DataServerServices();
    private PlanProccessServices _services = new PlanProccessServices();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["type"] != null && Request["type"].ToString() == "1")
            {
                lbl_Title.Text = Resources.Resource.DemandPlanningManagement;//需求计划管理
            }
            else
            {
                gvPlanView.Columns[15].Visible = false;

            }
        }
    }

    protected void btnUpd_Click(object sender, EventArgs e)
    {
        ImportPlanDomain domain = new ImportPlanDomain();
        domain.ID = Guid.Parse(hid_PlanID.Value);
        domain.MaterialCode = txtProductNo.Text.Trim();
        domain.m1 = ConvertDateType.IntParse(txt_Month1.Text.Trim());
        domain.m2 = ConvertDateType.IntParse(txt_Month2.Text.Trim());
        domain.m3 = ConvertDateType.IntParse(txt_Month3.Text.Trim());
        domain.m4 = ConvertDateType.IntParse(txt_Month4.Text.Trim());
        domain.m5 = ConvertDateType.IntParse(txt_Month5.Text.Trim());
        domain.m6 = ConvertDateType.IntParse(txt_Month6.Text.Trim());
        domain.m7 = ConvertDateType.IntParse(txt_Month7.Text.Trim());
        domain.m8 = ConvertDateType.IntParse(txt_Month8.Text.Trim());
        domain.m9 = ConvertDateType.IntParse(txt_Month9.Text.Trim());
        domain.m10 = ConvertDateType.IntParse(txt_Month10.Text.Trim());
        domain.m11 = ConvertDateType.IntParse(txt_Month11.Text.Trim());
        domain.m12 = ConvertDateType.IntParse(txt_Month12.Text.Trim());
        domain.Operation = this.UserName;
        domain.OperationTime = DateTime.Now;
        _services.UpdatePlan(domain);
        gvPlanView.DataBind();
    }
    protected void gvPlanView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImportPlanDomain DateItem = e.Row.DataItem as ImportPlanDomain;
            LinkButton lbl_xSVMaterial = e.Row.FindControl("lbl_xSVMaterial") as LinkButton;
            if (DateItem.UnitCount == 0 || DateItem.isTemp == 1)
            {
                List<SMT_ProductBaseInfoModel> list = new List<SMT_ProductBaseInfoModel>();
                list = _pservices.getProduct(lbl_xSVMaterial.Text);
                if (list != null && list.Count <= 1)
                {
                    if (list.Count == 1)
                    {
                        lbl_xSVMaterial.OnClientClick = "return AddRelation('" + list[0].ProductCode + "','" + list[0].HalfProductCode + "','" + list[0].ProgramName + "','" + list[0].ID + "','" + list[0].isTemp + "',"+list[0].UnitCount+")";
                    }
                    else
                    {
                        lbl_xSVMaterial.OnClientClick = "return AddRelation('" + lbl_xSVMaterial.Text + "','','','',0)";
                    }
                }
                else if (list != null && list.Count > 1)
                {
                    lbl_xSVMaterial.PostBackUrl = Page.ResolveClientUrl("~/BaseInfoManagement/MaterilMangement.aspx?product=" + list[0].ProductCode);
                }
                else
                {
                    lbl_xSVMaterial.OnClientClick = "return false";
                }


                if (DateItem.isTemp == 1)
                {
                    lbl_xSVMaterial.ForeColor = System.Drawing.Color.Orange;
                    lbl_xSVMaterial.Font.Bold = true;
                }

                if (DateItem.UnitCount == 0)
                {
                    lbl_xSVMaterial.ForeColor = System.Drawing.Color.Red;
                    lbl_xSVMaterial.Font.Bold = true;
                }


            }
            else
            {
                lbl_xSVMaterial.ForeColor = System.Drawing.Color.Black;
                lbl_xSVMaterial.OnClientClick = "return false";
            }
        }
    }
    protected void gvPlanView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            _services.DeletePlan(e.CommandArgument.ToString());
            gvPlanView.DataBind();
        }
    }

    protected void btn_SaveCopy_Click(object sender, EventArgs e)
    {
        if (_services.CopyVersion(this.dll_PlanVersion.Text, this.txt_VersionRemark.Text) == true)
        {
            ShowMessage("Copy Version Success");
        }
        else
        {
            ShowMessage("Copy Version Fail");
        }
    }

    protected void btn_Save_Click(object sender, EventArgs e)
    {
        SMT_ProductBaseInfoModel model = new SMT_ProductBaseInfoModel();

        if (string.IsNullOrEmpty(hid_ID.Value.Trim()))
        {
            model.ID = Guid.NewGuid().ToString();
        }
        else
        {
            model.ID = hid_ID.Value.Trim();
        }
        model.ProductCode = txtM_ProductCode.Text.Trim();
        model.UnitCount = ConvertDateType.IntParse(txtM_UnitCount.Text.Trim());
        model.Para = ConvertDateType.FloatTryFormat(txtM_Para.Text.Trim());
        model.Operation = this.UserName;
        model.OperationTime = DateTime.Now;
        if (cbx_isTemp.Checked == true)
        {
            model.isTemp = 1;
        }
        else
        {
            model.isTemp = 0;
        }

        if (_pservices.InsertOrUpdate(model) == true)
        {
            this.ShowMessage(Resources.Resource.InsertSuccess);//"插入成功！"
        }
        else
        {
            this.ShowMessage(Resources.Resource.InsertFailed);//"插入失败！"
        }
        gvPlanView.DataBind();
    }

    protected void btn_Delete_Plan_Click(object sender, EventArgs e)
    {
        if (_services.DeletePlanByVersion(this.dll_PlanVersion.Text) == true)
        {
            this.ShowMessage(Resources.Resource.DeleteSuccess);//"插入成功！"
        }
        else
        {
            this.ShowMessage(Resources.Resource.DeleteFailed);//"插入失败！"
        }
        gvPlanView.DataBind();
        dll_PlanVersion.DataBind();
    }
}